/* File:        	RequestCreate.java
* Package:			org.esi.authz.pdp.request
* Created:      	29-abr-2011
* Last Changed: 	0:10:49
* Author:       	Borja Urkizu borja.urquizu@tecnalia.com
*
*   
* History:
*  Revision 1.0.0.0  
* 
* Tecnalia Research and Innovation
*/
package com.tecnalia.authz.common.request.xacml;

import java.io.IOException;
import java.util.HashMap;

import org.jboss.security.xacml.core.model.context.RequestType;
import org.jboss.security.xacml.factories.RequestResponseContextFactory;
import org.jboss.security.xacml.interfaces.RequestContext;
import org.jboss.security.xacml.interfaces.XACMLConstants;

import com.tecnalia.authz.common.request.component.ActionAttrTypeImpl;
import com.tecnalia.authz.common.request.component.IRequestAuthorization;
import com.tecnalia.authz.common.request.component.RequestAuthorizationImpl;
import com.tecnalia.authz.common.request.component.ResourceAttrTypeImpl;
import com.tecnalia.authz.common.request.component.SubjectAttrTypeImpl;

// TODO: Auto-generated Javadoc
/**
 * The Class RequestCreate.
 */
public class XACMLRequestImpl implements IXACMLRequest {

	/* (non-Javadoc)
	 * @see org.esi.authz.pdp.request.IRequest#CreateRequest(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public RequestContext CreateRequest(String subjectID,
			String resID,
			String action,
			String roleGroup){
		
		RequestContext requestCtx = RequestResponseContextFactory.createRequestCtx();

		IRequestAuthorization req = null;
		req = new RequestAuthorizationImpl();
		//Set the subject
		HashMap<SubjectAttrTypeImpl, String> subjectData = new HashMap<SubjectAttrTypeImpl, String>();
		SubjectAttrTypeImpl subj = new SubjectAttrTypeImpl(XACMLConstants.ATTRIBUTEID_SUBJECT_ID);
		subjectData.put(subj, subjectID);
		//Set the group
		HashMap<SubjectAttrTypeImpl, String> groupData = new HashMap<SubjectAttrTypeImpl, String>();
		SubjectAttrTypeImpl group = new SubjectAttrTypeImpl(XACMLConstants.ATTRIBUTEID_ROLE);
		groupData.put(group, roleGroup);
		
		req.setSubject(subjectData, groupData);			
		
		/*HashMap<String, ResourceAttrTypeImpl> resourceData = new HashMap<String, ResourceAttrTypeImpl>();
		//Set the resource
		for (int i = 0; i < resID.length; i++) {
		ResourceAttrTypeImpl res = new ResourceAttrTypeImpl(XACMLConstants.ATTRIBUTEID_RESOURCE_ID);
		resourceData.put(resID[i], res);				
		}*/
		
		HashMap<String, ResourceAttrTypeImpl> resourceData = new HashMap<String, ResourceAttrTypeImpl>();
		ResourceAttrTypeImpl res = new ResourceAttrTypeImpl(XACMLConstants.ATTRIBUTEID_RESOURCE_ID);
		resourceData.put(resID, res);				
		req.setService(resourceData);

		
		//Set the action
		HashMap<ActionAttrTypeImpl, String> actionData = new HashMap<ActionAttrTypeImpl, String>();
		ActionAttrTypeImpl act = new ActionAttrTypeImpl(XACMLConstants.ATTRIBUTEID_ACTION_ID);
		actionData.put(act, action);
		req.setAction(actionData);
		RequestType requestType = req.getRequestType();
		
		try {
			requestCtx.setRequest(requestType);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return requestCtx;
	}

	
	
	/* (non-Javadoc)
	 * @see org.esi.authz.pdp.request.IRequest#CreateRequest(java.lang.String, java.lang.String[], java.lang.String, java.lang.String)
	 */
	@Override
	public RequestContext CreateRequest(String subjectID,
			String[] resID,
			String action,
			String roleGroup){

		RequestContext requestCtx = RequestResponseContextFactory.createRequestCtx();
	
		IRequestAuthorization req = null;
		req = new RequestAuthorizationImpl();
		//Set the subject
		HashMap<SubjectAttrTypeImpl, String> subjectData = new HashMap<SubjectAttrTypeImpl, String>();
		SubjectAttrTypeImpl subj = new SubjectAttrTypeImpl(XACMLConstants.ATTRIBUTEID_SUBJECT_ID);
		subjectData.put(subj, subjectID);
		//Set the group
		HashMap<SubjectAttrTypeImpl, String> groupData = new HashMap<SubjectAttrTypeImpl, String>();
		SubjectAttrTypeImpl group = new SubjectAttrTypeImpl(XACMLConstants.ATTRIBUTEID_ROLE);
		groupData.put(group, roleGroup);
		
		req.setSubject(subjectData, groupData);			
		
		HashMap<String, ResourceAttrTypeImpl> resourceData = new HashMap<String, ResourceAttrTypeImpl>();
		//Set the resource
		for (int i = 0; i < resID.length; i++) {
			ResourceAttrTypeImpl res = new ResourceAttrTypeImpl(XACMLConstants.ATTRIBUTEID_RESOURCE_ID);
			resourceData.put(resID[i], res);				
		}
		req.setService(resourceData);
		
		//Set the action
		HashMap<ActionAttrTypeImpl, String> actionData = new HashMap<ActionAttrTypeImpl, String>();
		ActionAttrTypeImpl act = new ActionAttrTypeImpl(XACMLConstants.ATTRIBUTEID_ACTION_ID);
		actionData.put(act, action);
		req.setAction(actionData);
		RequestType requestType = req.getRequestType();
		
		try {
			requestCtx.setRequest(requestType);
		} catch (IOException e) {
		// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return requestCtx;
	}
	
	/* (non-Javadoc)
	 * @see org.esi.authz.pdp.request.IRequest#CreateRequestAttrList(java.lang.String, java.lang.String[], java.lang.String, java.lang.String)
	 */
	@Override
	public RequestContext CreateRequestAttrList(String subjectID,
			String[] resID,
			String action,
			String roleGroup){
		
		RequestContext requestCtx = RequestResponseContextFactory.createRequestCtx();

		IRequestAuthorization req = null;
		req = new RequestAuthorizationImpl();
		//Set the subject
		HashMap<SubjectAttrTypeImpl, String> subjectData = new HashMap<SubjectAttrTypeImpl, String>();
		SubjectAttrTypeImpl subj = new SubjectAttrTypeImpl(XACMLConstants.ATTRIBUTEID_SUBJECT_ID);
		subjectData.put(subj, subjectID);
		//Set the group
		HashMap<SubjectAttrTypeImpl, String> groupData = new HashMap<SubjectAttrTypeImpl, String>();
		SubjectAttrTypeImpl group = new SubjectAttrTypeImpl(XACMLConstants.ATTRIBUTEID_ROLE);
		groupData.put(group, roleGroup);
		
		req.setSubject(subjectData, groupData);			
		
		/*HashMap<String, ResourceAttrTypeImpl> resourceData = new HashMap<String, ResourceAttrTypeImpl>();
		//Set the resource
		for (int i = 0; i < resID.length; i++) {
		ResourceAttrTypeImpl res = new ResourceAttrTypeImpl(XACMLConstants.ATTRIBUTEID_RESOURCE_ID);
		resourceData.put(resID[i], res);				
		}*/
		
		HashMap<String, ResourceAttrTypeImpl> resourceData = new HashMap<String, ResourceAttrTypeImpl>();
		ResourceAttrTypeImpl res = new ResourceAttrTypeImpl("urn:contur:xacml:2.0:resource:serviceList");
		String cadena = "";
		//Set the resource
		for (int i = 0; i < resID.length; i++) {
			cadena = cadena + resID[i];
			//For the last One dont put the ,
			if(i != (resID.length -1)){
				cadena = cadena + ",";
			}
		
		}
		
		
		resourceData.put(cadena, res);				
		req.setService(resourceData);
		
		//Set the action
		HashMap<ActionAttrTypeImpl, String> actionData = new HashMap<ActionAttrTypeImpl, String>();
		ActionAttrTypeImpl act = new ActionAttrTypeImpl(XACMLConstants.ATTRIBUTEID_ACTION_ID);
		actionData.put(act, action);
		req.setAction(actionData);
		RequestType requestType = req.getRequestType();
		
		try {
			requestCtx.setRequest(requestType);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return requestCtx;
	}

    /* (non-Javadoc)
	 * @see org.esi.authz.pdp.request.IRequest#CreateListRequest(java.lang.String, java.lang.String[], java.lang.String, java.lang.String)
	 */
    @Override
	public RequestContext[] CreateListRequest(String subjectID,
													String[] resID,
													String action,
													String roleGroup){
		
		RequestContext[] requestCtxList = new RequestContext[resID.length];
		
		//Set the resource
		for (int i = 0; i < resID.length; i++) {

			RequestContext requestCtx = RequestResponseContextFactory.createRequestCtx();

			IRequestAuthorization req = null;
			req = new RequestAuthorizationImpl();

			HashMap<String, ResourceAttrTypeImpl> resourceData = new HashMap<String, ResourceAttrTypeImpl>();
			ResourceAttrTypeImpl res = new ResourceAttrTypeImpl(XACMLConstants.ATTRIBUTEID_RESOURCE_ID);
			resourceData.put(resID[i], res);
			req.setService(resourceData);

			//Set the subject
			HashMap<SubjectAttrTypeImpl, String> subjectData = new HashMap<SubjectAttrTypeImpl, String>();
			SubjectAttrTypeImpl subj = new SubjectAttrTypeImpl(XACMLConstants.ATTRIBUTEID_SUBJECT_ID);
			subjectData.put(subj, subjectID);

			//Set the group
			HashMap<SubjectAttrTypeImpl, String> groupData = new HashMap<SubjectAttrTypeImpl, String>();
			SubjectAttrTypeImpl group = new SubjectAttrTypeImpl(XACMLConstants.ATTRIBUTEID_ROLE);
			groupData.put(group, roleGroup);
			
			req.setSubject(subjectData, groupData);	

			//Set the action
			HashMap<ActionAttrTypeImpl, String> actionData = new HashMap<ActionAttrTypeImpl, String>();
			ActionAttrTypeImpl act = new ActionAttrTypeImpl(XACMLConstants.ATTRIBUTEID_ACTION_ID);
			actionData.put(act, action);
			req.setAction(actionData);
			RequestType requestType = req.getRequestType();
			
			try {
				requestCtx.setRequest(requestType);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			requestCtxList[i] = requestCtx;
		}

		return requestCtxList;
		
	}



	
}
